package com.pillar.enty;

import com.pillar.game.NodeStatus;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Semaphore;
import java.util.stream.Collectors;


/**
 * @author pillar
 * @desc
 * @email: 347637454@qq.com
 * @createDate 2021-01-08 21:39
 */
@Data
public class Room implements Serializable {


    private String roomId;
    private String roomName;

    private final int MAX_PLAYER = 5;

    private final List<Player> playerList = new CopyOnWriteArrayList<>();
    private Semaphore semaphore = new Semaphore(MAX_PLAYER);
    public boolean fullPlayer(){
        return !semaphore.tryAcquire();
    }

    public Set<String> getPlayerChannel(){
        return playerList.stream().map(Player::getUuid).collect(Collectors.toSet());
    }


    public String getPlayerChannel(String uuid){
        return playerList.stream().filter(player -> player.getUuid().equals(uuid)).findFirst().map(Player::getUuid).orElse(null);
    }


    public void join(Player player){
        try {
            semaphore.acquire(1);
        } catch (InterruptedException ignored) {
        }
        playerList.add(player);
        if (playerList.size() == 1){
            return;
        }
        notifyJoin(player);
    }

    public void notifyJoin(Player player) {
        playerList.forEach(player1 -> {
            Channel channel = NodeStatus.findChannel(player1.getUuid());
           if (channel.isActive()){
               if (fullPlayer()){
                   channel.writeAndFlush(new TextWebSocketFrame("{\"cmd\":\"0x90\",\"version\":\"v1.0\",\"uuid\":\"992516908c1dffe8a6b8447764fbb291\",\"data\":\"玩家"+player.getUuid()+"加入房间,房间已满\"}"));
                   return;
               }
               channel.writeAndFlush(new TextWebSocketFrame("{\"cmd\":\"0x90\",\"version\":\"v1.0\",\"uuid\":\"992516908c1dffe8a6b8447764fbb291\",\"data\":\"玩家"+player.getUuid()+"加入房间\"}"));
           }
        });
    }

    public void notifyOut(Player player) {
        playerList.forEach(player1 -> {
            Channel channel = NodeStatus.findChannel(player1.getUuid());
           if (channel.isActive()){
               if (playerList.isEmpty()){
                   channel.writeAndFlush(new TextWebSocketFrame("{\"cmd\":\"0x90\",\"version\":\"v1.0\",\"uuid\":\"992516908c1dffe8a6b8447764fbb291\",\"data\":\"玩家"+player.getUuid()+"离开房间,游戏解散\"}"));
                   return;
               }
               channel.writeAndFlush(new TextWebSocketFrame("{\"cmd\":\"0x90\",\"version\":\"v1.0\",\"uuid\":\"992516908c1dffe8a6b8447764fbb291\",\"data\":\"玩家"+player.getUuid()+"离开房间\"}"));
           }
        });
    }

    public void out(Player player){
        if (playerList.isEmpty()){
            return;
        }
        playerList.remove(player);
        notifyOut(player);
        semaphore.release(1);
    }
}
